package ScatteredTest.manager;

import ScatteredTest.bean.Converter;
import ScatteredTest.bean.RequiredControl;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;

/**
 * @ClassName RequiredControlManager
 * @Description TODO
 * @Author ZhaoKai
 * @Date 2021-11-2511:13
 * @Version 1.0
 **/
public class RequiredControlManager {

    public static final double reductionPrincipal_MIN = 0;
    public static final double reductionPrincipal_MAX = 100000;

    public static final double overdueDays_MIN = 360;
    public static final double overdueDays_MAX = 720;


    public static final double deductibleInterestRatio_MIN = 50;
    public static final double deductibleInterestRatio_MAX = 80;

    public static final double overdueAmount_MIN = 10000;
    public static final double overdueAmount_First = 20000;
    public static final double overdueAmount_Second = 30000;
    public static final double overdueAmount_Third = 50000;
    public static final double overdueAmount_Forced = 70000;
    public static final double overdueAmount_Five = 100000;


    /**
     * 获取要求控制json
     * 获取必输控制json
     *
     * @param reductionPrincipal 减免本金
     * @param overdueDays        逾期天数
     * @param overdueAmount      申请减免金额
     * @param repaymentInterest  还款利息
     * @param cutInterestRate    减免利息
     * @return {@code JSONObject}
     */
    public static JSONObject getRequiredControlJson(double reductionPrincipal, double overdueDays, double overdueAmount, double repaymentInterest, double cutInterestRate) {

        //减免利息比
        double deductibleInterestRatio = cutInterestRate / (repaymentInterest + cutInterestRate) * 100;

        RequiredControl requiredControl = processData(reductionPrincipal, overdueDays, overdueAmount, deductibleInterestRatio);
        try {
            String requiredControlJsonString = Converter.toJsonString(requiredControl);
            return JSONObject.parseObject(requiredControlJsonString);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 处理数据
     *
     * @param reductionPrincipal      减少本金
     * @param overdueDays             逾期天数
     * @param overdueAmount           申请减免金额
     * @param deductibleInterestRatio 减免的利息比率
     * @return {@code RequiredControl}
     */
    private static RequiredControl processData(double reductionPrincipal, double overdueDays, double overdueAmount, double deductibleInterestRatio) {
        if (isFirstRule(reductionPrincipal, overdueAmount)) {
            return RequiredRulesEnum.FIRST.getRequired();
        }
        if (isSecondRule(reductionPrincipal, overdueDays, deductibleInterestRatio, overdueAmount)) {
            return RequiredRulesEnum.SECOND.getRequired();
        }

        if (isThirdRule(reductionPrincipal, overdueDays, deductibleInterestRatio, overdueAmount)) {
            return RequiredRulesEnum.THIRD.getRequired();
        }

        if (isFourthRule(reductionPrincipal, overdueDays, deductibleInterestRatio, overdueAmount)) {
            return RequiredRulesEnum.FOURTH.getRequired();
        }
        if (isFifthRule(reductionPrincipal, overdueDays, deductibleInterestRatio, overdueAmount)) {
            return RequiredRulesEnum.FIFTH.getRequired();
        }
        return null;
    }

    /**
     * 第一个规则
     *
     * @param reductionPrincipal 减少本金
     * @param overdueAmount      逾期金额
     * @return boolean
     */
    public static boolean isFirstRule(double reductionPrincipal, double overdueAmount) {
        System.out.println("reductionPrincipal:"+reductionPrincipal+"overdueAmount:"+overdueAmount);
        return reductionPrincipal >= reductionPrincipal_MAX
                || (reductionPrincipal <= reductionPrincipal_MIN && overdueAmount > overdueAmount_Five);
    }

    /**
     * 第二个规则
     *
     * @param reductionPrincipal      减少本金
     * @param overdueDays             逾期天数
     * @param deductibleInterestRatio 可扣除的利息比率
     * @param overdueAmount           逾期金额
     * @return boolean
     */
    public static boolean isSecondRule(double reductionPrincipal, double overdueDays, double deductibleInterestRatio, double overdueAmount) {
        return (reductionPrincipal > reductionPrincipal_MIN && reductionPrincipal < reductionPrincipal_MAX)
                || (reductionPrincipal <= reductionPrincipal_MIN && overdueDays <= overdueDays_MIN && deductibleInterestRatio < deductibleInterestRatio_MIN && overdueAmount > overdueAmount_Third && overdueAmount < overdueAmount_Five)
                || (reductionPrincipal <= reductionPrincipal_MIN && overdueDays <= overdueDays_MIN && deductibleInterestRatio >= deductibleInterestRatio_MIN && overdueAmount < overdueAmount_Five)
                || (reductionPrincipal <= reductionPrincipal_MIN && overdueDays > overdueDays_MIN && overdueDays <= overdueDays_MAX && deductibleInterestRatio < deductibleInterestRatio_MAX && overdueAmount > overdueAmount_Forced && overdueAmount < overdueAmount_Five)
                || (reductionPrincipal <= reductionPrincipal_MIN && overdueDays > overdueDays_MIN && overdueDays <= overdueDays_MAX && deductibleInterestRatio >= deductibleInterestRatio_MAX && overdueAmount < overdueAmount_Five);
    }

    /**
     * 第三个规则
     *
     * @param reductionPrincipal      减少本金
     * @param overdueDays             逾期天数
     * @param deductibleInterestRatio 可扣除的利息比率
     * @param overdueAmount           逾期金额
     * @return boolean
     */
    public static boolean isThirdRule(double reductionPrincipal, double overdueDays, double deductibleInterestRatio, double overdueAmount) {
        return reductionPrincipal <= reductionPrincipal_MIN && overdueDays <= overdueDays_MIN && deductibleInterestRatio < deductibleInterestRatio_MIN && overdueAmount > overdueAmount_Second
                || (reductionPrincipal <= reductionPrincipal_MIN && overdueDays > overdueDays_MIN && overdueDays <= overdueDays_MAX && deductibleInterestRatio < deductibleInterestRatio_MAX && overdueAmount > overdueAmount_Third)
                || (reductionPrincipal <= reductionPrincipal_MIN && overdueDays > overdueDays_MAX && overdueAmount > overdueAmount_Forced);
    }

    /**
     * 第四个规则
     *
     * @param reductionPrincipal      减少本金
     * @param overdueDays             逾期天数
     * @param deductibleInterestRatio 可扣除的利息比率
     * @param overdueAmount           逾期金额
     * @return boolean
     */
    public static boolean isFourthRule(double reductionPrincipal, double overdueDays, double deductibleInterestRatio, double overdueAmount) {
        return reductionPrincipal <= reductionPrincipal_MIN && overdueDays <= overdueDays_MIN && deductibleInterestRatio < deductibleInterestRatio_MIN && overdueAmount > overdueAmount_MIN
                || (reductionPrincipal <= reductionPrincipal_MIN && overdueDays > overdueDays_MIN && overdueDays <= overdueDays_MAX && deductibleInterestRatio < deductibleInterestRatio_MAX && overdueAmount > overdueAmount_First)
                || (reductionPrincipal <= reductionPrincipal_MIN && overdueDays > overdueDays_MAX && overdueAmount > overdueAmount_Second);
    }

    /**
     * 第五个规则
     *
     * @param reductionPrincipal      减少本金
     * @param overdueDays             逾期天数
     * @param deductibleInterestRatio 可扣除的利息比率
     * @param overdueAmount           逾期金额
     * @return boolean
     */
    public static boolean isFifthRule(double reductionPrincipal, double overdueDays, double deductibleInterestRatio, double overdueAmount) {
        return (reductionPrincipal <= reductionPrincipal_MIN && overdueDays <= overdueDays_MIN && deductibleInterestRatio < deductibleInterestRatio_MIN && overdueAmount <= overdueAmount_MIN)
                || (reductionPrincipal <= reductionPrincipal_MIN && overdueDays > overdueDays_MIN && overdueDays <= overdueDays_MAX && deductibleInterestRatio < deductibleInterestRatio_MAX && overdueAmount <= overdueAmount_First)
                || (reductionPrincipal <= reductionPrincipal_MIN && overdueDays > overdueDays_MAX && overdueAmount <= overdueAmount_Second);
    }
}
